<template>
	<scroll-view scroll-y="true" class="container" :data-theme="themeStyle">
		<view class="top_view">
			<image class="image" :src="'/static/login/ic_launcher_e.png'"></image>
		</view>
		<view class="header-wrap">
			<view class="title">登录</view>
			<view class="regisiter-agreement">
				<text class="color-tip">还没有账号,</text>
				<text class="color-base-text register-text" @click="toRegister">立即注册</text>
			</view>
		</view>
		<view class="body-wrap">
			<view class="form-wrap">
				<view class="input-wrap" v-show="loginMode == 'mobile'">
					<view class="content">
						<view class="area-code">+86</view>
						<input type="number" placeholder="仅限中国大陆手机号登录"
							placeholder-class="input-placeholder" class="input" maxlength="11"
							v-model="formData.mobile" />
					</view>
				</view>
				<view class="input-wrap" v-show="loginMode == 'account'">
					<view class="content">
						<input type="text" placeholder="请输入账号"
							placeholder-class="input-placeholder" class="input" maxlength="11"
							v-model="formData.account" />
						
					</view>
				</view>
				<view class="input-wrap" v-show="loginMode == 'account'">
					<view class="content">
						<input type="password" placeholder="请输入密码"
							placeholder-class="input-placeholder" class="input" v-model="formData.password" />
						<view class="align-right" v-show="loginMode == 'account'"><text @click="forgetPassword">忘记密码?</text></view>
					</view>
				</view>
				<view class="input-wrap" v-show="loginMode == 'mobile'">
					<view class="content">
						<input type="text" placeholder="请输入验证码"
							placeholder-class="input-placeholder" class="input" v-model="formData.vercode" />
						<!-- <image :src="captcha.img" class="captcha" @click="getCaptcha"></image> -->
					</view>
				</view>
				<view class="input-wrap" v-show="loginMode == 'mobile'">
					<view class="content">
						<input type="text" placeholder="请输入动态码" placeholder-class="input-placeholder" class="input"
							v-model="formData.dynacode" />
						<view class="dynacode" :class="dynacodeData.seconds == 120 ? 'color-base-text' : 'color-tip'"
							@click="sendMobileCode">{{ dynacodeData.codeText }}</view>
					</view>
				</view>
			</view>
			<view class="login-mode-box">
				<text @click="switchLoginMode"
					v-show="loginMode == 'mobile' && registerConfig.login.indexOf('username') != -1">使用账号登录</text>
				<text @click="switchLoginMode"
					v-show="loginMode == 'account' && registerConfig.login.indexOf('mobile') != -1">使用手机号登录</text>
			</view>
			<view class="btn_view">
				<!-- <button type="primary" @click="login"
					class="login-btn color-base-border ">登录（指定页面）</button> -->
				<button type="primary" @click="login"
						class="login-btn color-base-border ">登录</button>
			</view>
		</view>
		<!-- <loading-cover ref="loadingCover"></loading-cover> -->
		<!-- <register-reward ref="registerReward"></register-reward> -->
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				themeStyle: '', 
				height: 52, 
				current: 0,  
				loginMode: 'account',
				formData: { 
					account: '',
					password: '',
 
					mobile: '',
					vercode: '',

					dynacode: '',
					key: ''
				},
				isSub: false, // 提交防重复
				back: '', // 返回页
				redirect: 'redirectTo', // 跳转方式
				openidIsExits: false,
				isBind: false,

				registerConfig: {
					register: '',
					login: ''
				},
				dynacodeData: {
					seconds: 120
				}
			};
		},

		components: {},
		onLoad(option) {
 
		},

		methods: {
			   
			sendMobileCode() {},
			switchLoginMode() {

			},
			login1() {
				 this.$util.redirectTo('/pages/webview/webview', { type: '1' });
			},
			/**
			 * 登录
			 */
			login() { 
				 // this.$util.redirectTo('/pages/webview/webview', { type: '2' });
				 // return
				if (this.loginMode == 'account') {
					var api = this.api.login.loginAccountApi,
						data = {
							username: this.formData.account,
							password: this.formData.password
						};
					if (this.check.isNull(data.username, 'str')) {
						this.$util.showToast({
							title: '请输入账号'
						});
						return;
					}
					if (this.check.isNull(data.password, 'str')) {
						this.$util.showToast({
							title: '请输入密码'
						});
						return;
					}
				} else {
					var api = this.api.login.loginCodeApi,
						data = {
							mobile: this.formData.mobile,
							code: this.formData.ynacode
						};
					if (this.check.isNull(this.formData.mobile, 'str')) {
						this.$util.showToast({
							title: '请输入手机号'
						});
						return;
					}
					if (this.check.isNull(this.formData.dynacode, 'str')) {
						this.$util.showToast({
							title: '请输入验证码'
						});
						return;
					}
				}

				if (this.isSub) return;
				this.isSub = true;
				this.http.sendRequest({
					api,
					data,
					success: res => {
						if (res.code == 10000) {
							this.isSub = false;
							uni.setStorageSync(this.preutils.loginUser, data.account);
							uni.setStorageSync(this.preutils.loginPwd, data.password);
							// uni.setStorageSync(this.preutils.loginToken, res.data.memberinfo.token);
							uni.setStorageSync(this.preutils.userinfo, res.data);
							var memberinfo = uni.getStorageSync(this.preutils.userinfo)
							// var loginToken = uni.getStorageSync(this.preutils.loginToken);
   
							console.log(memberinfo)
							this.$util.showToast({
								title: '登录成功'
							});
							// this.$util.redirectTo('/pages/webview/webview', { type: '1' }); 
							console.log('-----d------')
							this.$util.redirectTo('/pages/webview/webview', { type: '1' });
							// if (this.back != '') {
							// 	console.log(this.back); 
							// 	this.$util.redirectTo(decodeURIComponent(this.back), {}, 'reLaunch');
							// } else {
							// 	this.$util.redirectToHome(false);
							// }
						} else {
							this.isSub = false;
							this.$util.showToast({
								title: res.msg
							});
						}
					},
					fail: res => {
						this.isSub = false;
					}
				});
			},
			/**
			 * 去注册
			 */
			toRegister() {
				if (this.back) this.$util.redirectTo('/pages/login/register/register', {
					back: this.back
				});
				else this.$util.redirectTo('/pages/login/register/register');
			},

			/**
			 * 忘记密码
			 */
			forgetPassword() {
				if (this.back) this.$util.redirectTo('/pages/login/forget/forget', {
					back: this.back
				});
				else this.$util.redirectTo('/pages/login/forget/forget');
			}
		},
		watch: {}
	};
</script>

<style lang="scss">
	@import '../public/css/common.scss';
	@import '../public/css/login.scss';
</style>

<style scoped>
	/deep/ .reward-popup .uni-popup__wrapper-box {
		background: none !important;
		max-width: unset !important;
		max-height: unset !important;
		overflow: unset !important;
	}

	/deep/ uni-toast .uni-simple-toast__text {
		background: red !important;
	}
</style>